﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Zwms.Client.CommonUI;
using Zwms.Data.BusinessObjects;

namespace Zwms.Client.ManageWms
{
    public partial class TransferBillDtlForm : BaseForm
    {
        private BilTransferBillDetail _currRecord;
        private bool _isNew = false;
        private string _barCode = string.Empty;
        private string _singleGoodsCode = string.Empty;
        private IList<BilTransferBillDetail> _billDtlList;
        public IList<BilTransferBillDetail> BillDtlList { get { return _billDtlList; } }
        public BilTransferBillDetail SelectedRecord { get { return _currRecord; } }

        public TransferBillDtlForm(BilTransferBillDetail record,bool isNew)
        {
            InitializeComponent();
            _billDtlList = new List<BilTransferBillDetail>();
            if (isNew)
            {
                this._currRecord = new BilTransferBillDetail();
                this._isNew = true;
            }
            else
            {
                this._currRecord = record;
                this._isNew = false;
                this.SetEditForm();
                this.SetUserControlValue(record);
            }
            this.InitButtonImage();
            
        }

        private void InitButtonImage()
        {
            this.btnAddConfirm.Image = global::Zwms.Client.Properties.Resources.ok;
            this.btnAdd.Image = global::Zwms.Client.Properties.Resources.add;
            this.btnCancel.Image = global::Zwms.Client.Properties.Resources.cancel;
        }

        private void SetEditForm()
        {
            this.btnAdd.Visible = false;
            this.btnAdd.Enabled = false;
            this.btnAddConfirm.Visible = false;
            this.tbsStockCode.SetEditState(false);
        }

        private void SetUserControlValue(BilTransferBillDetail record)
        {
            this.tbsStockCode.SetTextBoxContent(record.StockCode);
            this.tbGoodsCode.Text = record.GoodsCode;
            this.tbGoodsName.Text = record.GoodsName;
            this.nudPlanPkgQty.Value = record.PlanPkgGoodsQty;
            this.nudPlanPieceQty.Value = record.PlanPieceGoodsQty;
            this.nudTsfPkgQty.Value = record.PkgGoodsQty;
            this.nudTsfPieceQty.Value = record.PieceGoodsQty;
            this.tbSourceArea.Text = record.SourceAreaName;
            this.tbSourceArea.Tag = record.SourceAreaCode;
            this.tbSourceLocation.Text = record.SourceLocationName;
            this.tbSourceLocation.Tag = record.SourceLocationCode;
            this.tbTargetArea.Text = record.TargetAreaName;
            this.tbTargetArea.Tag = record.TargetAreaCode;
            this.tbsTargetLocation.SetTextBoxContent(record.TargetLocationName);
            this.tbsTargetLocation.Tag = record.TargetLocationCode;
            this.tbIBCode.Text = record.IBCode;
        }

        private void SetCurrTsfRecord()
        {
            
            _currRecord.StockCode = this.tbsStockCode.GetTextBoxContent();
            _currRecord.GoodsCode = this.tbGoodsCode.Text.Trim();
            _currRecord.GoodsName = this.tbGoodsName.Text.Trim();
            _currRecord.PlanPkgGoodsQty = (Int32)this.nudPlanPkgQty.Value;
            _currRecord.PlanPieceGoodsQty = (Int32)this.nudPlanPieceQty.Value;
            _currRecord.SourceAreaCode = this.tbSourceArea.Tag.ToString();
            _currRecord.SourceAreaName = this.tbSourceArea.Text;
            _currRecord.SourceLocationCode = this.tbSourceLocation.Tag.ToString();
            _currRecord.SourceLocationName = this.tbSourceLocation.Text;
            _currRecord.TargetAreaCode = this.tbTargetArea.Tag.ToString();
            _currRecord.TargetAreaName = this.tbTargetArea.Text;
            _currRecord.TargetLocationCode = this.tbsTargetLocation.Tag.ToString();
            _currRecord.TargetLocationName = this.tbsTargetLocation.GetTextBoxContent();
            _currRecord.IBCode = this.tbIBCode.Text.Trim();
            _currRecord.GoodsBarCode = this._barCode;
            _currRecord.SingleGoodsCode = this._singleGoodsCode;
        }

        #region 控件函数
        private void tbsStockCode_SelectButtonClick(object sender,EventArgs e)
        {
            ManageWms.GoodsStockSelForm window = new GoodsStockSelForm(true);
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.tbsStockCode.SetTextBoxContent(window.SelectedRecord.Id);
                this.tbGoodsCode.Text = window.SelectedRecord.GoodsCode;
                this.tbGoodsName.Text = window.SelectedRecord.GoodsName;
                this.nudPlanPkgQty.Value = window.SelectedRecord.PackageQty;
                this.nudPlanPieceQty.Value = window.SelectedRecord.PieceQty;
                this._barCode = window.SelectedRecord.GoodsBarCode;
                this._singleGoodsCode = window.SelectedRecord.SingleGoodsCode;

                this.tbSourceArea.Text =window.SelectedRecord.AreaName;
                this.tbSourceArea.Tag = window.SelectedRecord.AreaCode;
                this.tbSourceShelf.Text = window.SelectedRecord.ShelfName;
                this.tbSourceShelf.Tag = window.SelectedRecord.ShelfCode;
                this.tbSourceLocation.Text = window.SelectedRecord.LocationName;
                this.tbSourceLocation.Tag = window.SelectedRecord.LocationCode;
                this.tbIBCode.Text = window.SelectedRecord.IBCode;
            }
        }

        //private void tbsTargetArea_SelectButtonClick(object sender,EventArgs e)
        //{
        //    InfoWms.AreaWnd uc = new InfoWms.AreaWnd();
        //    CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择目标库区");
        //    uc.SetQueryState();
        //    if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        //    {
        //        this.tbsTargetArea.SetTextBoxContent(uc.SelectedRecord.AreaName);
        //        this.tbsTargetArea.Tag = uc.SelectedRecord.AreaCode;
        //    }
        //}

        //private void tbsTargetShelf_SelectButtonClick(object sender, EventArgs e)
        //{
        //    InfoWms.ShelfWnd uc = new InfoWms.ShelfWnd();
        //    CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择目标货架");
        //    uc.SetQueryState();
        //    if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        //    {
        //        this.tbsTargetShelf.SetTextBoxContent(uc.SelectedRecord.ShelfName);
        //        this.tbsTargetShelf.Tag = uc.SelectedRecord.ShelfName;
        //    }
        //}

        private void tbsTargetLocation_SelectButtonClick(object sender,EventArgs e)
        {
            InfoWms.LocationWnd uc = new InfoWms.LocationWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择目标库位");
            uc.SetQueryState();
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.tbsTargetLocation.SetTextBoxContent(uc.SelectedRecord.LocationName);
                this.tbsTargetLocation.Tag = uc.SelectedRecord.LocationCode;
                this.tbTargetArea.Text = uc.SelectedRecord.AreaName;
                this.tbTargetArea.Tag = uc.SelectedRecord.AreaCode;
                this.tbTargetShelf.Text = uc.SelectedRecord.ShelfName;
                this.tbTargetShelf.Tag = uc.SelectedRecord.ShelfCode;
            }
        }
        #endregion

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrWhiteSpace(this.tbsStockCode.GetTextBoxContent().Trim()))
            {
                MessageBox.Show("未选择记录，不能添加！");
                return;
            }
            if (MessageBox.Show("确定要添加该记录？", "确定", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
            {

                this.SetCurrTsfRecord();
                this._billDtlList.Add(this._currRecord);
            }
        }

        private void btnAddConfirm_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(this.tbsStockCode.GetTextBoxContent().Trim()))
            {
                MessageBox.Show("未选择记录，不能添加！");
                return;
            }
            if (MessageBox.Show("确定要添加该记录？", "确定", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
            {
                if (tbsTargetLocation.GetTextBoxContent() == "")
                {
                    MessageBox.Show("请选择目标库区！");
                    return;
                }
                this.SetCurrTsfRecord();
                this._billDtlList.Add(this._currRecord);
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }

        }

        //private void btnConfirm_Click(object sender, EventArgs e)
        //{
        //    if (this._isNew == true)
        //    {
        //        this.DialogResult = System.Windows.Forms.DialogResult.OK;
        //    }
        //    else
        //    {
        //        this.SetCurrTsfRecord();
        //        this.DialogResult = System.Windows.Forms.DialogResult.OK;
        //    }
        //}

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

    }
}
